<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Index</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="defaultf.css" rel="stylesheet" type="text/css" media="all" />
<link rel="stylesheet" type="text/css" href="http://localhost/Eldercare/jquery/easyui.css">
    <link rel="stylesheet" type="text/css" href="http://localhost/Eldercare/jquery/icon.css">
    <script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery-1.6.min.js"></script>
	<script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery.easyui.min.js"></script>
</head>
<body>
<div id="wrap">
<div id="header-wrapper">
<div id="header" class="container">
	<div id="logo">
	<img src="http://localhost/Eldercare/images/eldercare.gif" alt="" title="" border="0" width="270" height="70"/>
</div>
</p>
<br/>
<br/>
</div>
</div>
</div>
<div id="menu-wrapper">
	<div id="menu">
		<ul>
			<li class="current_page_item" ><a href="http://localhost/Eldercare/firstphp.php" accesskey="1" title="">Login</a></li>
		</ul>
	</div>
	</div>
<div>

<div id="wrap-content" style="margin:0px auto 0px auto; padding: 10px">
<form action="firstphp.php" method="post" name="formLogin" style="margin:0px auto 0px auto;"><fieldset>
<p></p>
<p></p>
<p style="vertical-align:middle;text-align:center">
<b>Username:</b>
<input id="loginusername" class="tb10" type="text" name="loginusername" size="20" maxlength="40" />
</p>
<p style="vertical-align:middle;text-align:center">
<b>Password:</b>
<input id="password" class="tb10" type="password" name="password" size="20" maxlength="40" value="" />
</p>

<div align="center">
<style type="text/css">
    .btn {
        background-color:beige;
        cursor:pointer;
    }
</style>
<button class="btn">Login</button>

</div>
<?php
session_start();
	if (!isset($_SESSION["check"]))
	{
		$_SESSION["check"] = 0;
	}
	else if($_SESSION["check"] == 1)
	{	
		echo "<p align='center'><font color =red>Please login to continue"; 
		$_SESSION["check"] = 0;
	}
	if (!empty($_POST))
	{
		if (!empty($_POST['loginusername']) && !empty($_POST['password']))
		{
			$loginusername = $_POST['loginusername'];
			$password = $_POST['password']; 
			$passHashed = md5($password);
			
			include 'conn.php';
			$qry = "SELECT username,password FROM user WHERE username='$loginusername' and password='$passHashed'";
			$result = mysql_query($qry);
			//$num = mysql_num_rows($result);
			
			if (mysql_num_rows($result)== 1)
			{
				while ($row = mysql_fetch_array($result)) 
				{		
					session_start();
					
					
					$qry = "Select centre.CentreName from user, centre where user.CentreId = centre.CentreId and user.Username = '$loginusername'";
					$result = mysql_query($qry);

					while($row = mysql_fetch_array($result))
					{
						$_SESSION["centreName"] = $row['CentreName'];
						
					}
					$uqry = "Select UserType from user where user.Username = '$loginusername'";
					$uresult = mysql_query($uqry);
					while($row = mysql_fetch_array($uresult))
					{
						$_SESSION["userType"] = $row['UserType'];
						
					}
					
					$_SESSION["loginusername"] = $loginusername;
					$_SESSION["password"] = $password;
					break;

					while($row = mysql_fetch_array($result))
					 {
						 $_SESSION["centreName"] = $row['CentreName'];
					 }
					 $_SESSION["loginusername"] = $loginusername;
					 $_SESSION["password"] = $password;
					 break;
				}
				mysql_close($conn);
				header("Location: AttendanceHome.php");
				
				
			}
			else
			{

				echo "<p align='center'> <font color=red> Please check your credentials and try again.";
			}
		}
		else if (empty($_POST['loginusername']))
		{
			echo "<p align='center'> <font color=red> Username cannot be empty.";
		}
		else if (empty($_POST['password']))
		{
			echo "<p align='center'> <font color=red> Password cannot be empty.";
		}
	}
       
?>
<p style="vertical-align:middle;text-align:center">
</p>
<!-- <br><a href="./CMSUForgetpassword.php">Forget password</br> -->
</fieldset>
</form>
</div>
</div>
</body>
</html>